package com.yunyao.web.controller.system;

import com.yunyao.common.core.BaseContext;
import com.yunyao.common.core.base.BaseController;
import com.yunyao.common.core.redis.RedisCache;
import com.yunyao.common.web.util.YResult;
import com.yunyao.framework.service.system.MenuService;
import com.yunyao.framework.vo.system.MenuVO;
import com.yunyao.web.utils.TreeListUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author chenyejian
 * @date 2024-07-16
 */
@RestController
@RequestMapping("/system/menu")
public class MenuController extends BaseController {

    @Autowired
    private MenuService sysMenuService;

    @Autowired
    private RedisCache redisCache;


    /**
     * getMenuList
     *
     * @param request
     * @return YResult
     */
    @GetMapping("/getMenuList")
    public YResult<List> getMenuList(HttpServletRequest request) {
        String usrId = BaseContext.getUserId();
        //查询该用户下的所有菜单
        try {
            List<MenuVO> sysMenus = sysMenuService.getMenuListByUserId(usrId);
            if (sysMenus==null && sysMenus.size() == 0) {
                return YResult.error("该用户没有菜单");
            }
            return YResult.success(TreeListUtil.list2Tree(sysMenus));
        } catch (Exception e) {
            logger.error("查询菜单异常", e);
            return YResult.error(e.getMessage());
        }
    }
}
